package cn.jinjimi.app;

import android.test.AndroidTestCase;

import cn.jinjimi.base.util.LogUtil;

import java.util.Random;

/**
 * Created by Administrator on 2015/3/23.
 */
public class AccountManagerTest extends AndroidTestCase{

    private AccountManager mAccountMgr = null;

    private Random mRand = null;

    private int mTestCount = 0;

    private String mRealName = "";

    public AccountManagerTest(){
        mAccountMgr = AccountManager.getInstance();
        mRealName = mAccountMgr.getAccount().realName;
        mRand = new Random();
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        LogUtil.println("setUp...");
    }


    private void updateSin(){
        String name = "zhangsan"+mRand.nextInt(500);
        LogUtil.println("...updateItem name : " + name);
        mAccountMgr.updateAccount(AccountManager.NameKey.REALNAME, name);
        synchronized (mRealName) {
            mRealName = name;
        }
        LogUtil.println(".... updateItem Succ " + name);
    }

    private void getInfo(){
        LogUtil.println(".... getInfo start");
        String name = mAccountMgr.getAccount().realName;
        LogUtil.println(".... getInfo of RealName : " + name +"/"+mRealName);
//        synchronized (mRealName) {
//            Assert.assertEquals(mRealName, name);
//        }
        String comp = mAccountMgr.getAccount().companyName;
        LogUtil.println(".... getInfo of companyName : " + comp);
    }

    private void doTest(){
            new Thread(){
                public void run(){
                    int i = 0;
                    while(i < 100) {
                        i++;
                        try {
                            sleep(100);
                        } catch (Exception e) {
                        }
                        int type = mRand.nextInt(500);
                        if (type % 3 == 1) {
                            updateSin();
                        }else{
                            getInfo();
                        }
                    }
                    mTestCount++;
                }
            }.start();
    }


    public void testGetAndSetInfo() {
        LogUtil.println("testGetAndSetInfo...");
        doTest();
        doTest();
        doTest();
        doTest();
        doTest();
        while(mTestCount != 5){
            try{
                Thread.sleep(20);
            }catch(Exception e){}
        }

    }

    public void stestUpdateInfo(){
        LogUtil.println("testUpdateInfo...");
    }

    @Override
    public void tearDown() throws Exception {
        super.tearDown();
        LogUtil.println("tearDown...");
    }
}
